<?php 
 require_once ("../include.php");
 $dests1 = sqlreadkeyarray("select itemid,formatted_address from location");
 $destarray1 = array();
 foreach($dests1 as $id=>$value) {$destarray1[] = $value[1];}
 $destarray2 = $destarray1;
 /* TODO Two parties:
 
 1. Destination of Person looking for passenger or driver.
 2. Destinations of list of free drivers or passengers.
 
 Match them and find all acceptable lifts.
 
 3. Choose the nearest.
?>

<!DOCTYPE html>
<pre>
<html>
 <head>
 <style>
table, td, th
{
border:1px solid green;
}

 </style>
  <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
	
<script>
	var service = new google.maps.DistanceMatrixService();

service.getDistanceMatrix(
  {
    origins: <?php echo json_encode($destarray1)?>,
    destinations: <?php echo json_encode($destarray2)?>,
    travelMode: google.maps.TravelMode.DRIVING
	 }, callback);

function callback(response, status) {
 if (status == google.maps.DistanceMatrixStatus.OK) {
  var outtable = '<table><tr><th></th>';
  document.getElementById('ans2').innerHTML = JSON.stringify(response,"*",1);
  for (var c=0;c< response.originAddresses.length;c++) {
	 outtable = outtable + '<th>' + response.originAddresses[c] + '</th>'
	}

  for (var r=0;r <response.destinationAddresses.length;r++) {
	  var destname = response.destinationAddresses[r] ;
	  outtable = outtable + '<tr><th>' + destname + '</th>'
			
    for (var c=0;c< response.originAddresses.length;c++) {
		 destitem = response.rows[c];
     outtable = outtable + '<td>' + destitem.elements[r].distance.text + '</td>'
  	}
		
	  outtable = outtable + '</tr>'
	}
	
  outtable = outtable + '</tr>';
  outtable = outtable + '</table>';
  document.getElementById('ans').innerHTML = outtable;

 }
 
}
</script>
</head>
<body>

<div id=ans>
</div>
<div id=ans2>
</div>
</body>
</html>